package com.drug.service.impl;

import com.drug.domain.*;
import com.drug.mapper.DrugInformationMapper;
import com.drug.mapper.PodiPdMapper;
import com.drug.mapper.PurchaseOrderMapper;
import com.drug.mapper.SupplierMapper;
import com.drug.service.StatisticsService;
import com.drug.util.Array;
import com.drug.util.PinyinUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private PodiPdMapper podiPdMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private DrugInformationMapper drugInformationMapper;
    @Autowired
    private SupplierMapper supplierMapper;

    /**
     * 采购单查询导出
     * @return
     */
    @Override
    public List<PurchaseOrder> findAllExport() {
        List<PurchaseOrder> purchaseOrders= purchaseOrderMapper.selectAll1();
        return purchaseOrders;
    }

    /**
     * 采购单模糊查询
     * @param statistics1
     * @return
     */
    @Override
    public Result statisticsFindAllPd(Statistics1 statistics1) {
        Result result = new Result();
        Integer pageNum=1;
        pageNum  = statistics1.getPageNum();
        if (pageNum==null){
            pageNum=1;
        }
        //通用名
        String daName = statistics1.getDiName();
        if (daName==null||daName.equals("")){
            daName=null;
        }else {
            //获取所有的通用名
            String[] strings = drugInformationMapper.selectAllName();
            for (int i = 0; i < strings.length; i++) {
                String pinYinHeadChar = PinyinUtils.getPinYinHeadChar(strings[i]);
                String substring = pinYinHeadChar.substring(0,1);
                if (substring.equals(daName)){
                    statistics1.setDiName(strings[i]);
                }
            }
        }
        PageHelper.startPage(pageNum,5);
        List<PodiPd> podiPds=podiPdMapper.selectAllStatistics(statistics1);
        PageInfo pageInfo = new PageInfo(podiPds);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 根据供货商模糊搜索
     * @param statistics2
     * @return
     */
    @Override
    public Result statisticsFindAllSp(Statistics2 statistics2) {
        Result result = new Result();
        Integer pageNum = 1;
        pageNum = statistics2.getPageNum();
        if (pageNum == null) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 5);
        String daSupplier = statistics2.getDaSupplier();
        if (daSupplier == null || daSupplier == "") {
            daSupplier = "1";
            statistics2.setDaSupplier(daSupplier);
        }
        List<Supplier> podiPds = supplierMapper.selectAllStatistics(statistics2);
        PageInfo pageInfo = new PageInfo(podiPds);
        result.setData(pageInfo);
        return result;
    }
    /**
     * 根据供货商查询药品信息
     *
     */
    @Override
    public Result findOneDiBySp(Integer surId){
        Result result = new Result();
        String[] strings = podiPdMapper.selectOneDiBySp(surId);
        Integer[] ints = Array.StringToInt(strings);
        List<DrugInformation> drugInformationList= drugInformationMapper.selectDiOne(ints);
        result.setData(drugInformationList);
        return result ;
    }

    @Override
    public List<Supplier> findAllExport1() {
        Statistics2 statistics2 = new Statistics2();
        return supplierMapper.selectAllStatistics(statistics2);
    }

    @Override
    public Result statisticsFindAllHs(Statistics3 statistics3) {
        Result result = new Result();
        Integer pageNum = 1;
        pageNum = statistics3.getPageNum();
        if (pageNum == null) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 5);
        List<PodiPd> podiPds = podiPdMapper.selectAllHs(statistics3);
        PageInfo pageInfo = new PageInfo(podiPds);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 根据医院查询药品详细信息
     * @param hId
     * @return
     */
    @Override
    public Result findOneDiByHs(Integer hId) {

        Result result = new Result();
        String[] strings = podiPdMapper.selectOneDiByHs(hId);
        Integer[] ints = Array.StringToInt(strings);
        List<DrugInformation> drugInformationList= drugInformationMapper.selectDiOne(ints);
        result.setData(drugInformationList);
        return result ;
    }
}
